<?php
namespace app\plus_exam\model;
use think\Model;

/**
 * 考试类型
 * Class examcategory
 * @package app\exam\model
 */
class Question extends Model{
    protected $pk       = 'id';
    protected $table    = 'sp_plus_exam_question';
    protected $autoWriteTimestamp = true;
    protected $pageNumber = 20; 

    /**
     * 单个
     * @param int $param
     * @return array
     */
    public function find_info($param) {
        return self::get($param);
    }

    /**
     * 列表
     * @access public
     * @param array $where
     */
    public function select_lists($where,$keyword){
        if($keyword){
            return $this->where($where)->where('content','like','%'.$keyword.'%')->order('id desc')->paginate($this->pageNumber,false,['query' =>$where,'keyword' => $keyword]);
        }else{
            return $this->where($where)->order('id desc')->paginate($this->pageNumber,false,['query' =>$where]);
        }
    }

    /**
     * 列表
     * @access public
     * @param array $where
    */
    public function select_lists_api($category_id,$question_number){
        return $this->query("SELECT * FROM {$this->table} WHERE category_id ={$category_id} AND id >= ((SELECT MAX(id) FROM {$this->table})-(SELECT MIN(id) FROM {$this->table})) * RAND() + (SELECT MIN(id) FROM {$this->table}) LIMIT $question_number");
    }
 
  
    /**
     * 添加
     * @param array $param
     * @return int
     */
    public function add_data($param) {
        $data = [
            'category_id'   => $param['category_id'],
            'type'      => $param['type'],
            'content'   => $param['content'],
            'options'   => $param['options'],
            'answer'    => $param['answer'],
            'create_time' => $param['create_time'],
            'update_time' => $param['update_time'],
        ];
        return $this->insertGetId($data);
    }
    
    /**
     * 编辑
     * @param type $param
     */
    public function edit_data($param) {
        $data = [
            'content'     => $param['content'],
            'options'     => $param['options'],
            'answer'      => $param['answer'],
            'update_time' => $param['update_time'],
        ];
        $where = ['id'=>$param['id']];
        return $this->where($where)->update($data);
    }
    
    /**
     * 删除数据
     * @param int $id
     */
    public function delete_data($id) {
        return $this->destroy($id);
    }

    /**
     * $user_anwer = [105 => 'A',106 => 'B']
     * $point 每题多少分;
     */
    public function get_score($user_answer,$point) {
        $user_answer_question_ids = array_keys($user_answer);
        foreach($user_answer_question_ids as $key => $val) {
            $user_answer_question_ids[$key] = intval($val);
        }
        $question_ids = implode(',', array_keys($user_answer));
        $raw_sys_answer = $this->where('id','in',$question_ids)->order('id','desc')->select();
        $sys_answer = array();
        foreach($raw_sys_answer as $answer) {
            $sys_answer[$answer['id']] = $answer['answer'];
        }
        $count = 0;
        foreach($user_answer as $key => $val) {
            if($val === $sys_answer[$key]) {
                $count = $count+$point;
            }
        }
        return $count;
    }
    
}
